/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.groupbuy.platform.task;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.yami.shop.groupbuy.comment.model.GroupActivity;
import com.yami.shop.groupbuy.comment.model.GroupTeam;
import com.yami.shop.groupbuy.comment.service.GroupActivityService;
import com.yami.shop.groupbuy.comment.service.GroupTeamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 拼团任务定时任务
 *
 * @author 小懒虫
 * @date 2019/9/3 11:41
 */
@Component("groupBuyTask")
public class GroupBuyTask {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private GroupTeamService groupTeamService;

    @Autowired
    private  GroupActivityService groupActivityService;



    /**
     * 拼团活动/团订单结束任务
     */
    public void activityFinish() {
        logger.info("取消拼团活动/团订单结束的任务。。。");
        // 获取活动结束或团单结束未成团的拼团团队
        List<GroupTeam> groupTeams = groupTeamService.getNotGroupTeam();
        if (groupTeams.size() > 0) {
            // 处理未成团的拼团团队，返回退款单列表
            for (GroupTeam groupTeam : groupTeams) {
                groupTeamService.updateNotGroupTeam(groupTeam);
            }
        }
    }

    /**
     * 拼团活动/团订单结束任务
     */
    public void activityFinishAndProdChange() {
        logger.info("拼团活动结束，团购商品恢复为普通商品。。。");
        // 获取活动结束或团单结束未成团的拼团团队
        List<GroupActivity> groupActivitieList = groupActivityService.list(new LambdaQueryWrapper<GroupActivity>()
                .lt(GroupActivity::getEndTime,new Date())
                .ne(GroupActivity::getStatus,5)
                .ne(GroupActivity::getStatus, -1)
        );
        List<Long> groupActivityIds = Lists.newArrayList();
        for (GroupActivity groupActivity: groupActivitieList){
            groupActivityIds.add(groupActivity.getGroupActivityId());
        }
        if (CollUtil.isNotEmpty(groupActivityIds)){
            groupActivityService.changeProdTypeByGroupActivityIdList(groupActivityIds);
        }
    }


}
